<?php
use SF\Controller as C;

class C_Abstract
{
    const SESS_ADMIN_NAME = 'admin_name';
    
    /**
     * @var Smarty
     */
    public $smt = null;
    /**
     * @var \SF\Db\Db
     */
    public $db = null;

    protected $_page = 1;
    protected $_perpage = 40;
    protected $_limit = '';
    
    public function __construct()
    {
        $c = C\Router::getController();
        if (empty($_SESSION[self::SESS_ADMIN_NAME]) && $c != 'login') {
            C\Router::redirect('/?c=login');
        }
        $this->smt = getSmarty('admin');
        $this->smt->assign('base', getBaseUrl());
        $this->smt->assign('admin_name', isset($_SESSION[self::SESS_ADMIN_NAME]) ? $_SESSION[self::SESS_ADMIN_NAME] : '');
        $this->db = getDb();

        // 分页相关的参数和处理
        $this->_page = getRequest('page', '1', 'int');
        $this->_limit = ' LIMIT '. ($this->_page - 1) * $this->_perpage .','. $this->_perpage;
        $this->smt->assign('page', $this->_page);
    }


    protected function getCount()
    {
        static $count = null;
        if (null === $count) {
            $count = $this->db->fetchOne('SELECT FOUND_ROWS()');
        }
        return $count;
    }

    protected function getPageHtml()
    {
        $count = $this->getCount();
        $page_html = '';
        if ($count > $this->_perpage) {
            $PG = new \SF\Widgets\Paginator($this->_page, $count, $this->_perpage);
            $page_html = $PG->getRender();
        }
        return $page_html;
    }
}
